using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.RenderGraphModule;

namespace LiteRP
{
    public partial class LiteRenderGraphRecorder
    {
        private static readonly ProfilingSampler s_DrawEditorGizmoPassProfilingSampler = new ProfilingSampler("Draw Editor Gizmo Pass");
        
        internal class DrawEditorGizmoPassData
        {
            internal RendererListHandle gizmoRendererListHandle;
        }
        
        private void AddDrawEditorGizmoPass(RenderGraph renderGraph, CameraData cameraData, GizmoSubset gizmoSubset)
        {
            if (!Handles.ShouldRenderGizmos() ||cameraData.camera.sceneViewFilterMode == Camera.SceneViewFilterMode.ShowFiltered)
            {
                return;
            }
            using (var builder = renderGraph.AddRasterRenderPass<DrawEditorGizmoPassData>("Draw Editor Gizmo Pass", out var passData, s_DrawEditorGizmoPassProfilingSampler))
            {
                passData.gizmoRendererListHandle = renderGraph.CreateGizmoRendererList(cameraData.camera, gizmoSubset);
                builder.UseRendererList(passData.gizmoRendererListHandle);

                if (m_BackbuffColorTextureHandle.IsValid())
                {
                    builder.SetRenderAttachment(m_BackbuffColorTextureHandle, 0, AccessFlags.Write);
                }

                if (m_BackbuffDepthTextureHandle.IsValid())
                {
                    builder.SetRenderAttachmentDepth(m_BackbuffDepthTextureHandle, AccessFlags.Read);
                }

                
                builder.AllowPassCulling(false);
                
                builder.SetRenderFunc((DrawEditorGizmoPassData data, RasterGraphContext context) =>
                {
                    context.cmd.DrawRendererList(data.gizmoRendererListHandle);
                });
            }
        }
    }
}